package com.xianzw.mnfun.task;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.xianzw.mnfun.model.domain.ConstellationFortune;
import com.xianzw.mnfun.model.enums.Constellation;
import com.xianzw.mnfun.repository.ConstellationFortuneRepository;
import com.xianzw.mnfun.utils.DateUtils;
import com.xianzw.mnfun.utils.HttpUtils;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

@Slf4j
@Component
public class ConstellationFortuneTask {
    
    //AppCode
    private static final String AppCode = "7535968156a74b858b676a1a1424dfa7";
    
    private static final String AppCode1 = "3bf2a4b373034cca969a01b71980a02d";
    
    @Autowired
    private ConstellationFortuneRepository constellationFortuneRepository;

    @Scheduled(cron = "0 20 1 * * *")
    public void getRequest() {
	    String date = DateUtils.dateToString(new Date(), DateUtils.DEFAULT_DATE_PATTERN);

	    Constellation[] values = Constellation.values();
	    for (Constellation constellation : values) {
		String id = constellation.getId();
		String desc = constellation.getDesc();
		String descName = constellation.getDescName();
    	    getConstellation1(id,desc,descName,date); 
	    }
	    
	}
    
    public void getConstellation1(String id,String desc,String descName,String date) {
	 String host = "https://ali-star-lucky.showapi.com";
	    String path = "/star";
	    String method = "GET";
	    String appcode = AppCode1;
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("needMonth", "1");
	    querys.put("needTomorrow", "1");
	    querys.put("needWeek", "1");
	    querys.put("needYear", "1");
	    querys.put("star", desc);

	    try {
		
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	
	    	JSONObject object = JSONObject.fromObject(EntityUtils.toString(response.getEntity()));
	    	if("0".equals(object.get("showapi_res_code").toString())) {
	    	    JSONObject result = JSONObject.fromObject(object.get("showapi_res_body"));
	    	    
	    	    ConstellationFortune constellationFortune = new ConstellationFortune();
	    	    
	    	    constellationFortune.setAstroid(Integer.valueOf(id));
	    	    constellationFortune.setAstroName(descName);
	    	    
	    	    constellationFortune.setYear(result.getJSONObject("year").toString());
	    	    constellationFortune.setWeek(result.getJSONObject("week").toString());
	    	    constellationFortune.setToday(result.getJSONObject("day").toString());
	    	    constellationFortune.setTomorrow(result.getJSONObject("tomorrow").toString());
	    	    constellationFortune.setMonth(result.getJSONObject("month").toString());
	    	    
		    constellationFortune.setDate(date);
		    constellationFortuneRepository.save(constellationFortune);
	    	}else {
	    	    log.info("星座运势获取失败");
	    	}
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	
    }
    
    
    
    
    public void getConstellation(String id,String date) {
	 String host = "https://jisuastro.market.alicloudapi.com";
	    String path = "/astro/fortune";
	    String method = "GET";
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + AppCode);
	
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("astroid", id);
	    querys.put("date", date);

	    try {
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	
	    	JSONObject object = JSONObject.fromObject(EntityUtils.toString(response.getEntity()));
	    	if("ok".equals(object.get("msg"))) {
	    	    JSONObject result = JSONObject.fromObject(object.get("result"));
	    	    
	    	    ConstellationFortune constellationFortune = new ConstellationFortune();
	    	    
	    	    constellationFortune.setAstroid(Integer.valueOf(id));
	    	    constellationFortune.setAstroName(result.getString("astroname"));
	    	    constellationFortune.setYear(result.getJSONObject("year").toString());
	    	    constellationFortune.setWeek(result.getJSONObject("week").toString());
	    	    constellationFortune.setToday(result.getJSONObject("today").toString());
	    	    constellationFortune.setTomorrow(result.getJSONObject("tomorrow").toString());
	    	    constellationFortune.setMonth(result.getJSONObject("month").toString());
	    	    
		    constellationFortune.setDate(date);
		    constellationFortuneRepository.save(constellationFortune);
	    	}else {
	    	    log.info("星座运势获取失败");
	    	}
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
    }
}
